#include "sourceline.h"
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <iomanip>

//class LineError
	int LineError::getColumn(){
		return m_column;
	}

	char* LineError::getError(){
		return m_errorMsg;
	}

	void LineError::set(int col, char* err){		// Set column number and error
		m_column = col;
		m_errorMsg = err;
	}


//class SourceLine {


	void SourceLine::clearLine(){					// clears the line
		m_line = new char[m_maxLen];
		m_line[0] = '\0';
	}

	void SourceLine::printErrors(){					// prints error messages in current line{
		//TODO: implement...
	}

	SourceLine::SourceLine(int size){				// constructor
		m_maxLen = size;
		m_line = new char[size];
		m_firstLexemeColumn = 0;
		m_lastLexemeColumn = 0;
		m_lineErrors = 0;
		m_lineNo = 1;
		m_line[0] = '\0';
		this->addLineNumbers();
	}

	void SourceLine::buildLine(const char* str){	// builds the line
		strcat(m_line, str);
		m_lastLexemeColumn += strlen(str);
		//std::cout << "Column: " << m_lastLexemeColumn << std::endl;
	}

	void SourceLine::printLine(){					// prints the line and error messages in line if any
		std::cout << m_line  << std::endl;
		if( m_lineErrors > 0 )
		{
			for( int i = 0; i < m_lineErrors; i++ )
			{
				int lexemeCol = m_errors[i].getColumn() + 1;
				for( int j = 0; j < lexemeCol; j++ )
				{
					std::cout << " ";
				}
				std::cout << "^ " << m_errors[i].getError() << std::endl;
			}
		}
		m_lineErrors = 0;
	}

	void SourceLine::setError(char* msg){			// sets an error message
		LineError error;
		error.set( m_lastLexemeColumn, msg );
		m_errors[m_lineErrors] = error;
		m_lineErrors++;
	}

	void SourceLine::clearError()
	{
		m_lineErrors--;
	}

	void SourceLine::newLine(){						// announces a new line in the input
		m_firstLexemeColumn = 0;
		m_lastLexemeColumn = 0;
		m_lineNo ++;
		//std::cout << m_line << std::endl;  //Af hverju settum við ÞETTA hér?!?!?!?!?
		this->addLineNumbers();
	}

	void SourceLine::addLineNumbers(){
		std::stringstream ss;
		ss.fill(' ');
		ss.width(5);
		ss << m_lineNo << ":  ";
		strcat(m_line, ss.str().c_str());
	}

